import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';
import handleAllpks from "./handleAllpks";
import {agreeFormTypeMap, custFormToServer, formToServer} from "./configData";
import {setFormAttrAndValue} from "../../../../../public/functions/checkFormAction";
import checkBtnPermission from "./checkBtnPermission";

export default function editAgree(record) {
    const {orgVal} = this.state;
    checkBtnPermission(record.pk_agreement.value, 'edit', 'agreement', () => {
        proFetch({
            url: '/nccloud/hrcm/account/EditContractAction.do',
            body: {
                pk_org: orgVal.refpk,
                selectedPk: record.pk_agreement.value,
                isContract: false
            },
        }).then((res) => {
            if (res.success) {
                setData.call(this, record)
            }
        })
    })
}

function setData(record) {
    let typeConf = agreeFormTypeMap[record.conttype.value][record.agreementtype.value];
    const {json, orgVal} = this.state;
    let mainArea, areas, postData;
    if (typeConf) {
        mainArea = typeConf.mainArea;
        areas = typeConf.areas;
        postData = formToServer[mainArea];
    } else {
        mainArea = record.agreementtype.value;
        areas = mainArea + 'Card';
        if (record.conttype.value === '3') {
            areas = ['agreementCard', areas + 'Before', areas + 'After'];
        } else {
            areas = [areas];
        }
        postData = {
            ...custFormToServer[record.conttype.value],
            areaCode: !typeConf && record.conttype.value === '3' ? areas.join(','):  areas[0]
        };
    }
    const agreeAppCode = postData.appCode;
    postData = {
        ...postData,
        pk_org: orgVal.refpk,
        pk_psndoc_sub: record.pk_agreement.value,
        tempFlag: true,
        isContract: false
    };
    proFetch({
        url: '/nccloud/hrcm/common/QueryDataCardAction.do',
        body: postData,
    })
        .then((res) => {
            if (res.success) {
                let template = this.props.meta.getMeta();
                let isRadio = false;
                areas.forEach(item => {
                    /*if (item.indexOf('CardBefore') > -1) {
                        res.data.template[item].items.forEach(item2 => {
                            item2.attrcode = item2.attrcode + '_old';
                            item2.disabled = true;
                        });
                    }*/
                    const ifpropItem = res.data.template[item].items.find(item2 => item2.attrcode === 'ifprop');
                    isRadio = ifpropItem && ifpropItem.itemtype === 'radio';
                    template[item] = res.data.template[item];
                });
                const [area, ...subArea] = areas;
                let editArea = area;
                if (subArea.length) {
                    template['formrelation'] = {
                        ...(template['formrelation'] || {}),
                        [area]: subArea
                    };
                    editArea = subArea[subArea.length - 1];
                }
                let registed = this.registed;
                for (let key in template) {
                    if(template.hasOwnProperty(key) && template[key].moduletype){
                        if(!registed.includes(template[key].code)) {
                            template[key].moduletype === 'form' && this.props.use.form(template[key].code);
                            template[key].moduletype === 'table' && this.props.use.editTable(template[key].code);
                        }
                　　}
                }
                this.props.meta.setMeta(template,()=>{
                    if (res.data.attrMap) {
                        setFormAttrAndValue(res.data.attrMap, res.data.valueMap, this.props, area, areas)
                    }
                });
                
                const allpks = this.props.table.getAllTableData('agreementList').rows.map(item => item.values.pk_agreement.value);
                handleAllpks.set(allpks);
                const areaValue = res.data.data[area];
                if (isRadio) {
                    if (areaValue.rows[0].values.hasOwnProperty('ifprop')) {
                        let ifprop = areaValue.rows[0].values.ifprop.value;
                        areaValue.rows[0].values.ifprop.value = ifprop ? 'Y' : 'N';
                        areaValue.rows[0].values.ifprop.display = ifprop ? json['cm6011-000057'] : json['cm6011-000058'];
                    }
                    if (areaValue.rows[0].values.hasOwnProperty('ifprop_old')) {
                        let ifprop_old = areaValue.rows[0].values.ifprop_old.value;
                        areaValue.rows[0].values.ifprop_old.value = ifprop_old ? 'Y' : 'N';
                        areaValue.rows[0].values.ifprop_old.display = ifprop_old ? json['cm6011-000057'] : json['cm6011-000058'];
                    }
                }
                this.setState({
                    agreeArea: area,
                    agreeAllpks: allpks,
                    agreeData: areaValue,
                    agreeAppCode,
                    agreementPage: 'edit',
                    lastAgreementPage: 'main',
                    agreeSubArea: subArea,
                    agreePageid: res.data.template.pageid
                }, () => {
                    this.props.form.setAllFormValue({[area]: areaValue});
                    this.props.form.setFormStatus(area, 'edit');
                    /*if (areaValue.rows[0].values.hasOwnProperty('ifprop')) {
                        //initFormMeta.call(this, 'agree', area, areaValue.rows[0].values.ifprop);
                        if (isRadio) {
                            let ifprop = areaValue.rows[0].values.ifprop.value;
                            let ifprop_old = areaValue.rows[0].values.ifprop_old && areaValue.rows[0].values.ifprop_old.value;
                            this.props.form.setFormItemsValue([area], {
                                ifprop: {value: ifprop ? 'Y' : 'N'}, ifprop_old: {value: ifprop_old ? 'Y' : 'N'}
                            });
                        }
                    }*/
                    if (subArea.length) {
                        subArea.forEach(item => this.props.form.openArea(item))
                    }
                    this.updateButtonStatus();
                })
            }
        });
}